/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Network;

import java.net.*;
import java.io.*;
import com.thoughtworks.xstream.*;
import GameLogic.*;

/**
 *
 * @author Martin
 */
public class Client implements Serializable
{
    private Socket socket;
    private BufferedInputStream in;
    private BufferedOutputStream out;

    public Client( String ip )
    {
        connect( ip , 1234 );
        close();
    }

    /*public void main( String[] args )
    {
        connect( "127.0.0.1" , 1234 );
        receive();
         close();
    }*/

    public void connect( String ip , int port )
    {
        try
        {
            System.out.println("connecting to Server...");
            System.out.println();
            socket =  new Socket( ip , port );
            getOutputStream();
            getInputStream();
            System.out.println("\tconnected to Server");
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in Connect() ; " + e.toString() );
        }
    }

    public void getInputStream()
    {
        try
        {
            System.out.println("creating Inputstream...");
            in = new BufferedInputStream( socket.getInputStream() );
            System.out.println("\tInputstream created");
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in getInputStream() ; " + e.toString() );
        }
    }

    public void getOutputStream()
    {
        try
        {
            System.out.println("creating Outputstream...");
            out = new BufferedOutputStream( socket.getOutputStream() );
            System.out.println("\tOutputstream created");
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in getOutputStream() ; " + e.toString() );
        }
    }

    public void close()
    {
        try
        {
            System.out.println("closing Server...");
            in.close();
            out.close();
            socket.close();
            System.out.println("\tServer closed");
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in close() ; " + e.toString() );
        }
    }

    public void send( Object obj )
    {
        try
        {
            System.out.println("sending message...");

            //Neues XStream-Objekt erstellen
            XStream xstream = new XStream();

            out.write( xstream.toXML( obj ).getBytes() , 0 , 1024 );
            out.flush();
            System.out.println("\tmessage sent");
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in send() ; " + e.toString());
        }
    }

    public void receive()
    {
        try
        {
            System.out.println("receiving message...");

            String str = "", xml = "";
            char c;

            c = (char)in.read();
            if( c == '#')
            {
                c = (char)in.read();
            }

            while( (int)c != 0 && c != '#' )
            {
                str += c;
                c = (char)in.read();
            }

            c = (char)in.read();
            xml += c;

            while( c != '#' && in.available() != 0 )
            {
                c = (char)in.read();
                xml += c;
            }

            if(str == "")
            {
                System.out.println("\tno message in buffer");
            }
            else
            {
                System.out.println("\tmessage received");
                interpret( str , xml );
            }
            System.out.println();
        }
        catch( Exception e )
        {
            System.out.println( "in receive() ; " + e.toString() );
        }
    }

    public static void interpret( String predata, String xml )
    {
        try
        {
            XStream xstream = new XStream();

            if( predata.compareTo("class GameLogic.Player") == 0)
            {
                System.out.println(xml);
                Player player = (Player)xstream.fromXML( xml );
                System.out.println(player.getSnakeOrientation());
            }
            else if( predata.compareTo("class GameLogic.Head") == 0)
            {
                Head head = (Head)xstream.fromXML( xml );
                System.out.println( head.getTexture());
            }
            else
            {
                System.out.println(predata);
            }
        }
        catch( Exception e )
        {
            System.out.println( e.toString() );
        }
    }

}
